<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>管理后台</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />

    <link rel="shortcut icon" href="{{url('manager/favicon.ico')}}" type="image/x-icon" />
    <link rel="stylesheet" href="{{url('manager/css/font.css')}}">
    <link rel="stylesheet" href="{{url('manager/css/xadmin.css')}}">

    <script src="{{url('manager/js/jquery.min.js')}}"></script>
    <script src="{{url('manager/lib/layui/layui.js')}}" charset="utf-8"></script>
    <script type="text/javascript" src="{{url('manager/js/xadmin.js')}}"></script>

</head>
<body class="form-wrap" >

<div class="layui-fluid"><xblock>
        <span class="x-right" >系统已录入公司@if($companys!=''){{count($companys)}}@else 0 @endif家</span>
    </xblock>
    <div class="layui-card">
        <div class="layui-card-header">为用户：“@if($managerInfo!=''){{$managerInfo->username}}@endif”&nbsp;绑定公司,该用户昵称为：@if($managerInfo!=''){{$managerInfo->nickname}}@endif</div>
        <div class="layui-card-body" style="padding: 15px;">
            <div><h2>请在列表中选择公司，如果该用户的企业信息未录入，请到用户管理-公司列表，点击‘新增公司’按钮，录入企业信息</h2></div>
            <br>

            <form class="layui-form" action="" lay-filter="component-form-group">

                <div class="layui-row">
                <div class="layui-form-item">
                    <label for="username" class="layui-form-label">
                        <span class="x-red">*</span>请选择公司
                    </label>
                    <div class="layui-input-inline">
                        <select name="contrller" id="company_id" lay-search>
                            @if(count($companys) > 0 && $companys!='')
                                <option value="">可以在框中输入关键词搜索</option>
                                @foreach($companys as $company)
                                    <option value="{{$company->id}}" > {{$company->company_name}}</option>
                                @endforeach
                            @endif
                        </select>
                        <input type="hidden" value="@if($managerInfo!=''){{$managerInfo->user_id}}@endif" id="user_id">
                        <input type="hidden" value="@if($managerInfo!=''){{$managerInfo->username}}@endif" id="user_name">
                    </div>
                </div>

                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button type="button" class="layui-btn" onclick="save()">保存</button>
                        </div>
                    </div>
                </div>
            </form>

            <p>
            <div>该用户已绑定<span class="x-red">@if($bindCompanyList!=''){{count($bindCompanyList)}} @else 0 @endif </span>家公司,分别是：
                <table class="layui-table">
                    <thead>
                    <tr>
                        <th>公司ID</th>
                        <th>公司名称</th>
                        <th>信用号</th>
                        <th>绑定时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    @if($bindCompanyList!='')
                        @foreach($bindCompanyList as $bindCompany)
                    <tr>
                        <th>{{$bindCompany->company_id}}</th>
                        <th>{{$bindCompany->company_name}}</th>
                        <th>{{$bindCompany->company_num}}</th>
                        <th>{{$bindCompany->updated_at}}</th>
                        <th><a href="javascript:;" onclick="unset(this,'{{$bindCompany->id}}','{{$bindCompany->company_id}}','{{$managerInfo->user_id}}')">解绑</a></th>
                    </tr>@endforeach
                    @else
                        <tr>无数据</tr>
                        @endif
                    </tbody>

                </table>

            </div></p>
        </div>
    </div>
</div>
<script>


    function unset(obj,id,com_id,user_id) {
        layer.confirm('确认解绑'+com_id+'？',function(index){
        $.ajax({
            url: '{{route("Api.Admin.Sys.companyUnbind")}}',
            type: 'post',
            dataType: 'json',
            data: {
                record_id:id,
                user_id: user_id,
                company_id: com_id,
            },
            success: function (obj) {
                if (obj.level == "success") {
                    layer.msg('解绑成功！');
                    setTimeout(function () {
                        location.replace(location.href);
                    },1500)
                } else if (obj.level == "error") {
                    layer.msg(obj.message);

                }

            },
            error: function (obj) {
                console.log(obj);
            }
        });
    })}
    function save(){
        var user_id=$("#user_id").val();
        var user_name=$("#user_name").val();
        var company_id = $("#company_id").val();
        layer.confirm('确认'+user_name+'的信息？',function(index){
            $.ajax({
                url: '{{route("Api.Admin.Sys.companyBind")}}',
                type: 'post',
                dataType: 'json',
                data: {
                    user_id: user_id,
                    company_id: company_id,
                },
                success: function (obj) {
                    if (obj.level == "success") {
                        layer.msg('保存成功！');
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.location.reload();
                        },1500)
                    } else if (obj.level == "error") {
                        layer.msg(obj.message);
                    }
                },
                error: function (obj) {
                    console.log(obj);
                }
            });
        })

    }
</script>
</body>
</html>
